package com.jingdianjichi.subject.domain.handler;

import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName :SubjectTypeHandlerFactory
 * Package :com.jingdianjichi.subject.domain.handler
 * Desciption :
 *
 * @Author :
 * @Create : 19:19
 * @Version :1.0
 */

@Component
public class SubjectTypeHandlerFactory implements InitializingBean {
//实现这个接口的原因是这个bean初始化后一定会执行afterPropertiesSet方法

    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlerList;//这样子注入的话我们所实现的四个类都会被注入进来

    private Map<SubjectInfoTypeEnum,SubjectTypeHandler> handlerMap =  new HashMap<>();


    //该参数前端会传进来
    public SubjectTypeHandler getHandler(int subjectType){
        SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
        return handlerMap.get(subjectInfoTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for(SubjectTypeHandler subjectTypeHandler:subjectTypeHandlerList){
            handlerMap.put(subjectTypeHandler.getHandlerType(),subjectTypeHandler);
        }
    }
}
